//最新 小程序开发api接口统一配置
// const API_BASE_URL = 'https://cloud.wanqiao.cn/api/'
const API_BASE_URL = 'http://192.168.102.137:8099/znfk/'
/*********************************公共******************************************/

const LOGIN_URL = "wxapp/login" //登录

/*********************封装get,post请求**********************************************/
const request = (url, method, data, type) => {
  let _url = API_BASE_URL + url
  return new Promise((resolve, reject) => {
    wx.request({
      url: _url,
      method: method,
      data: data,
      dataType: 'json',
      header: {
        'token': wx.getStorageSync('token'), //token为登录后台传
        'content-Type': type == 'form' ? 'application/x-www-form-urlencoded' : 'application/json;charset=UTF-8'
      },
      success(request) {
        resolve(request.data);
      },
      fail(error) {
        reject(error);
        wx.showToast({
          title: '服务器响应失败！',
          icon: 'none'
        })
      },
      complete(aaa) {
        // 加载完成
      }
    })
  })
}

/**
 * 小程序的promise没有finally方法，自己扩展下
 */
if (!Promise.prototype.finally) {
  Promise.prototype.finally = function (callback) {
    var Promise = this.constructor;
    return this.then(
      function (value) {
        Promise.resolve(callback()).then(
          function () {
            return value;
          }
        );
      },
      function (reason) {
        Promise.resolve(callback()).then(
          function () {
            throw reason;
          }
        );
      }
    );
  }
}

module.exports = {
  request,
  login: (data) => request(LOGIN_URL, 'post', data, 'json'), //登陆方法
}